<template>
    <!-- 老师预约咨询页面 -->
    <div class="app-container">
        <el-row>
            <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch">



                <el-form-item label="所在院部">
                    <el-select v-model="queryParams.deptId" placeholder="请选择" clearable class="elSelect" @change="handleChange"  style="width: 100%">
                        <el-option v-for="item in deptOptions" :key="item.deptId" :label="item.deptName"
                        :value="item.deptId"></el-option>
                    </el-select>
                    <!-- <el-cascader v-model="queryParams.deptId" :options="deptOptions"
                        :props="{ value: 'id', currentLabels: 'label', checkStrictly: true }" @change="handleChange"
                        @keyup.enter.native="search"></el-cascader> -->
                </el-form-item>

                <el-form-item label="所选专业">
                    <el-select v-model="queryParams.specialityId" clearable placeholder="请选择" @keyup.enter.native="search">
                        <el-option v-for="item in profession" :key="item.VALUE" :label="item.TITILE"
                            :value="item.VALUE"></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="咨询师">
                    <el-select v-model="queryParams.pcbId" clearable placeholder="请选择" @keyup.enter.native="search">
                        <el-option v-for="item in basicList" :key="item.VALUE" :label="item.label"
                            :value="item.value"></el-option>
                    </el-select>
                </el-form-item>


                <el-form-item label="选择日期">
                    <el-date-picker v-model="queryParams.aptStartTime" type="date" placeholder="选择日期"
                        value-format="yyyy-MM-dd">
                    </el-date-picker>
                </el-form-item>


                <el-form-item>
                    <div style="float:right;">
                        <el-button type="primary" icon="el-icon-search" @click="handleQuery" size="mini">搜索</el-button>
                        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
                    </div>
                </el-form-item>

            </el-form>
        </el-row>
        <el-table v-loading="loading" :data="typeList">
            <el-table-column label="状态" align="center" prop="status">
                <template slot-scope="scope">
                    {{ scope.row.status == 'W' ? '未读' : '已读' }}
                </template>
            </el-table-column>
            <el-table-column label="所在院系" align="center" prop="ds" :show-overflow-tooltip="true">
            </el-table-column>
            <el-table-column label="学生" align="center" prop="studentName" />
            <el-table-column label="注册号" align="center" prop="regCode" />

            <el-table-column label="性别" align="center" prop="sex" width="50">
                <template slot-scope="scope">
                    <dict-tag :options="dict.type.sys_user_sex" :value="scope.row.sex" />
                </template>
            </el-table-column>
            <el-table-column label="电话号码" align="center"></el-table-column>
            <el-table-column label="预约心理师" prop="consName" align="center"></el-table-column>
            <el-table-column label="预约时间段" prop="aptStartEndTimeStr" align="center" :show-overflow-tooltip="true"></el-table-column>

            <el-table-column label="预约状态" prop="aptStatus" align="center">
                <template slot-scope="scope">
                    <dict-tag :options="dict.type.psy_apt_status" :value="scope.row.aptStatus" />
                </template>
            </el-table-column>
            <el-table-column label="咨询人诉求" prop="consReqs" :formatter="reqType" align="center">
            </el-table-column>
            <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="130">
                <template slot-scope="scope">
                    <el-button size="mini" type="text" @click="handleUpdate(scope.row)">查看详情</el-button>
                    <el-button size="mini" type="text" @click="handleDelete(scope.row)"
                        v-if="scope.row.aptStatus != 'S' ? true : false">确定预约</el-button>
                </template>
            </el-table-column>
        </el-table>

        <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
            @pagination="getList" />

    </div>
</template>
    
<script>
import { deptTreeSelect } from "@/api/system/user";
import { getSpecialityByDeptId } from "@/api/information/unitMajor";
import { consaptList, getConsapt, confirmApt } from '@/api/psychological/reservation'
import { getDicts } from "@/api/system/dict/type";
import { consAll } from "@/api/psychological/basic"
import {
  collegeSelect
} from "@/api/information/studentInfor"


export default {
    name: "Dict",
    dicts: ['psy_cons_req_type', 'sys_user_sex', 'psy_apt_status'],
    data() {
        return {
            pickerOptions: {
                shortcuts: [{
                    text: '最近一周',
                    onClick(picker) {
                        const end = new Date();
                        const start = new Date();
                        start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
                        picker.$emit('pick', [start, end]);
                    }
                }, {
                    text: '最近一个月',
                    onClick(picker) {
                        const end = new Date();
                        const start = new Date();
                        start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
                        picker.$emit('pick', [start, end]);
                    }
                }, {
                    text: '最近三个月',
                    onClick(picker) {
                        const end = new Date();
                        const start = new Date();
                        start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
                        picker.$emit('pick', [start, end]);
                    }
                }]
            },
            // 遮罩层
            loading: false,
            showSearch: true,
            // 选中数组
            ids: [],
            // 非单个禁用
            single: true,
            // 非多个禁用
            multiple: true,
            // 显示搜索条件
            // 总条数
            total: 0,
            // 字典表格数据
            typeList: [],
            // 弹出层标题
            title: "",
            // 是否显示弹出层
            open: false,
            // 日期范围
            dateRanges: [],

            // 查询参数
            queryParams: {
                pageNum: 1,
                pageSize: 10,
                pcbId:undefined,
                dictName: undefined,
                dictType: undefined,
                status: undefined
            },
            // 表单参数
            form: {},
            deptOptions: [],
            profession: [],
            reqTypeList: [],
            basicList: []
        };
    },
    created() {
        getDicts("psy_cons_req_type").then(res => {
            this.reqTypeList = res.data.map(item => {
                return {
                    value: item["dictValue"],
                    label: item["dictLabel"]
                };
            });
        });
        consAll().then(res => {
            console.log(res)
            this.basicList = res.rows.map(item => {
                return {
                    value: item["pcbId"],
                    label: item["consName"]
                };
            });

        })
        this.getList();
        this.department();
    },
    methods: {
        /** 查询字典类型列表 */
        getList() {
            consaptList(this.queryParams).then(res => {
                console.log(res, '教师预约列表')
                this.typeList = res.rows;
                this.typeList.forEach(element => {
                    element.consReq = element.consReq.split()
                    console.log(element.consReq)
                });
                this.total = res.total;
                this.loading = false;
            })
        },
        // 咨询诉求
        reqType(row) {

            let colorArr = []; //新建一个数组
            row.consReqs.forEach((item, index) => {
                this.reqTypeList.forEach(i => {
                    if (item == i.value) {
                        colorArr.push(i.label + ';')
                        return colorArr
                    }
                })
            });
            return colorArr;
        },
        // 获取部门信息
        async department() {
            await collegeSelect().then(res => {
                this.deptOptions = res.data;
            });
        },
        // 取消按钮
        cancel() {
            this.open = false;
        },

        /** 搜索按钮操作 */
        handleQuery() {
            this.queryParams.pageNum = 1;
            this.queryParams.pageSize = 10;
            this.getList();
        },
        /** 重置按钮操作 */
        resetQuery() {
            // this.dateRange = [];
            this.queryParams = {}
            this.handleQuery();
        },
        // 下拉树选择
        handleChange(value) {
            this.form.deptId = value
            this.queryParams.deptId = value
            this.form.specialityId = "";
            this.getSpeciality();
            // if (typeof value == "object") {
            //     this.form.deptId = value[value.length - 1];
            //     this.queryParams.deptId = value[value.length - 1];
            //     this.getSpeciality();
            // } else if (typeof value == "number") {
            //     this.form.deptId = value;
            //     this.getSpeciality();
            // } else if (value == undefined) {
            //     this.form.specialityId = "";
            // }
        },
        getSpeciality() {
            if (this.form.deptId) {
                getSpecialityByDeptId({ deptId: this.form.deptId }).then(res => {
                    if (res.code == "200") {
                        this.profession = res.data;
                    } else {
                        console.log("123");
                        this.profession = [];
                    }
                });
            } else {
                this.profession = [];
            }
        },

        /** 新增按钮操作 */
        handleAdd() {
            this.open = true;
            this.title = "添加";
        },
        // 多选框选中数据
        handleSelectionChange(selection) {
            this.ids = selection.map(item => item.dictId)
            this.single = selection.length != 1
            this.multiple = !selection.length
        },
        /** 修改按钮操作 */
        handleUpdate(row) {
            getConsapt(row.pcaId).then(res => {
                console.log(res, '查询详情')
                if (res.code == 200) {
                    this.$router.push({ path: '/psychological/detailsView', query: res.data })
                }

            })

        },
        /** 提交按钮 */
        submitForm: function () {
            this.$refs["form"].validate(valid => {
                if (valid) {

                }
            });
        },
        /** 确定预约操作 */
        handleDelete(row) {
            this.$modal
                .confirm('是否确认预约心理的学生' + row.studentName + '到心理部门咨询？')
                .then(function () {
                    return confirmApt(row);
                })
                .then(() => {
                    this.$modal.msgSuccess("确认成功");
                    this.getList()
                })
                .catch(() => { });

        },
    }
};
</script>
